package com.leadbank.example.controller;

import com.leadbank.example.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * Created by hp on 2019/4/24.
 */
@RequestMapping("/movies")
@RestController
@Slf4j
public class MovieController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/users/{id}")
    @HystrixCommand(fallbackMethod = "queryUserByIdFallback")
    public User findByid(@PathVariable Long id) {
        long begin = System.currentTimeMillis();
        // 这里用到了RestTemplate的占位符能力
        User user = restTemplate.getForObject("http://microservice-simple-provider-user/users/{id}",User.class,id);
        // ...电影微服务的业务...
        long end = System.currentTimeMillis();
        // 记录访问用时：
        log.info("访问用时：{}", end - begin);
        return user;
    }

    public User queryUserByIdFallback(Long id){
        User user = new User();
        user.setId(id);
        user.setName("用户信息查询出现异常！");
        return user;
    }
}
